package classloader;

import com.myservlet.HttpServlet;

import java.io.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MyClassLoaderByClass extends MyClassLoaderByJar {
    private String classpath;
    private URL[] urls;
    private ModuleManager manager;

    public MyClassLoaderByClass(String classpath, URL[] urls, ModuleManager manager) {
        super(urls, manager);
        this.classpath = classpath;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        try {
            byte [] classDate=getDate(name);

            if(classDate==null){}

            else{
                //defineClass方法将字节码转化为类
                return defineClass(name,classDate,0,classDate.length);
            }

        } catch (IOException e) {

            e.printStackTrace();
        }

        return super.findClass(name);
    }

    //返回类的字节码
    private byte[] getDate(String className) throws IOException{
        InputStream in = null;
        ByteArrayOutputStream out = null;
        String path=classpath + File.separatorChar +
                className.replace('.',File.separatorChar)+".class";
        try {
            in=new FileInputStream(path);
            out=new ByteArrayOutputStream();
            byte[] buffer=new byte[2048];
            int len=0;
            while((len=in.read(buffer))!=-1){
                out.write(buffer,0,len);
            }
            return out.toByteArray();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        finally{
            in.close();
            out.close();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        List<String> jarList = new ArrayList<>();
        jarList.add("/Users/admin/webapps/webdemo1/WEB-INF/lib/mysevlet-api-1.0-SNAPSHOT.jar");
        jarList.add("/Users/admin/webapps/webdemo1/WEB-INF/lib/myutils-1.0-SNAPSHOT.jar");
        ModuleManager manager = new ModuleManager();
        manager.setModuleList(jarList);
        manager.init();


        MyClassLoaderByClass myClassLoaderByClass = new MyClassLoaderByClass(
                "/Users/admin/webapps/webdemo1/WEB-INF/classes/",
                new URL[]{new File("/Users/admin/webapps/webdemo1/WEB-INF/lib/mysevlet-api-1.0-SNAPSHOT.jar").toURI().toURL()},
                manager
        );
        Class c = myClassLoaderByClass.loadClass("com.lagou.servlets.UserServlet");
        Object o = c.newInstance();
        System.out.println(o.getClass().getClassLoader());
        System.out.println(o.getClass().getClassLoader().getParent());
        System.out.println(o.getClass().getClassLoader().getParent().getParent());
        for (Method declaredMethod : c.getDeclaredMethods()) {
            System.out.println(declaredMethod.getName());
        }
        HttpServlet httpServlet = (HttpServlet) o;
        System.out.println(httpServlet);

        System.out.println(myClassLoaderByClass.getResource("/WEB-INF"));
        InputStream resourceAsStream = myClassLoaderByClass.getResourceAsStream("web.xml");
        String str = new String(resourceAsStream.readAllBytes());
        System.out.println(str);


        System.out.println(myClassLoaderByClass.getClass().getClassLoader());
        System.out.println(myClassLoaderByClass.getClass().getClassLoader().getParent());
        System.out.println(myClassLoaderByClass.getClass().getClassLoader().getParent().getParent());
    }
}
